import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import matplotlib.pyplot as plt
import seaborn as sns

# 加载数据
data_path = r"D:\python\Binary classification\Logistic Regression\cs-training.csv"
df = pd.read_csv(data_path)

# 查看数据的一些基本信息
print("数据前5行：")
print(df.head())
print("\n数据基本信息：") 
print(df.info())

# 检查缺失值
print("\n缺失值情况：")
print(df.isnull().sum())

# 填充缺失值或删除含缺失值的行
df = df.dropna()  # 或者使用 df.fillna(df.mean()) 填充

# 特征与标签分离
# 假设最后一列是目标变量 'Risk'
X = df.drop(columns=['SeriousDlqin2yrs'])  # X 为特征
y = df['SeriousDlqin2yrs']  # y 为目标变量

# 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 创建随机森林分类器
rf = RandomForestClassifier(n_estimators=100, random_state=42)

# 训练模型
rf.fit(X_train, y_train)

# 预测
y_pred = rf.predict(X_test)

# 评估模型
print("\n分类报告：")
print(classification_report(y_test, y_pred))

print("\n准确率：")
print(accuracy_score(y_test, y_pred))

# 混淆矩阵
cm = confusion_matrix(y_test, y_pred)

# 绘制混淆矩阵
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Negative', 'Positive'], yticklabels=['Negative', 'Positive'])
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()

# 特征重要性
feature_importances = rf.feature_importances_
features = X.columns

# 绘制特征重要性图
plt.figure(figsize=(10, 6))
sns.barplot(x=feature_importances, y=features)
plt.title('Feature Importance')
plt.xlabel('Importance')
plt.ylabel('Features')
plt.show()
